package com.fmdj.mis.api.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.fmdj.common.util.CommonResult;
import com.fmdj.common.util.CosUtil;
import com.fmdj.common.util.PageUtils;
import com.fmdj.mis.api.controller.form.SelectDriverAuthInfoForm;
import com.fmdj.mis.api.controller.form.SelectDriverByPageForm;
import com.fmdj.mis.api.feign.DrServiceApi;
import com.fmdj.mis.api.service.DriverService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;

@Service
public class DriverServiceImpl implements DriverService {
    @Resource
    private DrServiceApi drServiceApi;

    @Resource
    private CosUtil cosUtil;

    @Override
    public PageUtils selectDriverByPage(SelectDriverByPageForm form) {
        CommonResult commonResult = drServiceApi.selectDriverByPage(form);
        HashMap map= (HashMap) commonResult.get(commonResult.RETURN_RESULT);
        PageUtils pageUtils= BeanUtil.toBean(map, PageUtils.class);
        return pageUtils;
    }

    @Override
    public HashMap selectDriverAuthInfo(byte realAuth, Long driverId) {
        HashMap map = new HashMap();
        /**
         * realAuth
         *  1未认证（认证失败），2已认证，3审核中
         *  后台管理员在认证审批的时候拒绝该司机的realAuth就会=1，需要司机重新提交资料才会更新为3
         * */
        if(realAuth ==2 || realAuth==3) {
            SelectDriverAuthInfoForm form = new SelectDriverAuthInfoForm();
            form.setDriverId(driverId);
            CommonResult r = drServiceApi.selectDriverAuthInfo(form);
            //返回值
            HashMap summaryMap = (HashMap) r.get(CommonResult.RETURN_RESULT);
            /*
             * 先取出6张图片
             * */
            String idcardFront = MapUtil.getStr(summaryMap, "idcardFront");
            String idcardBack = MapUtil.getStr(summaryMap, "idcardBack");
            String idcardHolding = MapUtil.getStr(summaryMap, "idcardHolding");
            String drcardFront = MapUtil.getStr(summaryMap, "drcardFront");
            String drcardBack = MapUtil.getStr(summaryMap, "drcardBack");
            String drcardHolding = MapUtil.getStr(summaryMap, "drcardHolding");
            /*
             * 将上面的图片生成临时的外网访问URL
             * */
            idcardFront = idcardFront.length() > 0 ? cosUtil.getPrivateFileUrl(idcardFront) : "";
            idcardBack = idcardBack.length() > 0 ? cosUtil.getPrivateFileUrl(idcardBack) : "";
            idcardHolding = idcardHolding.length() > 0 ? cosUtil.getPrivateFileUrl(idcardHolding) : "";
            drcardFront = drcardFront.length() > 0 ? cosUtil.getPrivateFileUrl(drcardFront) : "";
            drcardBack = drcardBack.length() > 0 ? cosUtil.getPrivateFileUrl(drcardBack) : "";
            drcardHolding = drcardHolding.length() > 0 ? cosUtil.getPrivateFileUrl(drcardHolding) : "";
            /*
             * 保存到结果集中
             * */
            summaryMap.replace("idcardFront",idcardFront);
            summaryMap.replace("idcardBack", idcardBack);
            summaryMap.replace("idcardHolding", idcardHolding);
            summaryMap.replace("drcardFront", drcardFront);
            summaryMap.replace("drcardBack", drcardBack);
            summaryMap.replace("drcardHolding", drcardHolding);
            //返回结果集
            map.put(CommonResult.DRIVER_AUTH_INFO,summaryMap);
        }
        return map;
    }

}